import { computed } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useNpiStore } from "@/store/modules/hmi/npi.store";
import { useErrorHandler } from "./useErrorHandler";
import { NpiClearType } from "@/api/hmi/npi.api";

/**
 * NPI 数据清零 Composable
 */
export function useNpiClear() {
  // Store
  const npiStore = useNpiStore();
  
  // 错误处理
  const { handleError } = useErrorHandler();

  // 计算属性
  const isClearing = computed(() => npiStore.isClearing);
  const isClearingUser = computed(() => npiStore.isClearingUser);
  const isClearingNpi = computed(() => npiStore.isClearingNpi);
  const lastUserClearResult = computed(() => npiStore.lastUserClearResult);
  const lastNpiClearResult = computed(() => npiStore.lastNpiClearResult);

  /**
   * 用户数据清零
   * @param cpu CPU 编号
   */
  const clearUserData = async (cpu: number = 1) => {
    try {
      // 显示确认对话框
      await ElMessageBox.confirm(
        "确定要执行用户数据清零操作吗？此操作不可撤销。", 
        "确认清零", 
        {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        }
      );

      // 执行清零操作
      const result = await npiStore.clearUserData(cpu);

      // 根据响应结果提示用户
      if (result.status === "Success") {
        ElMessage.success("用户数据清零成功");
        return true;
      } else {
        ElMessage.error("用户数据清零失败");
        return false;
      }
    } catch (error: any) {
      if (error === "cancel") {
        ElMessage.info("已取消用户数据清零操作");
        return false;
      } else {
        handleError(error, "用户数据清零");
        return false;
      }
    }
  };

  /**
   * NPI 数据清零
   * @param cpu CPU 编号
   */
  const clearNpiData = async (cpu: number = 1) => {
    try {
      // 显示确认对话框
      await ElMessageBox.confirm(
        "确定要执行 NPI 数据清零操作吗？此操作不可撤销。", 
        "确认清零", 
        {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        }
      );

      // 执行清零操作
      const result = await npiStore.clearNpiData(cpu);

      // 根据响应结果提示用户
      if (result.status === "Success") {
        ElMessage.success("NPI 数据清零成功");
        return true;
      } else {
        ElMessage.error("NPI 数据清零失败");
        return false;
      }
    } catch (error: any) {
      if (error === "cancel") {
        ElMessage.info("已取消 NPI 数据清零操作");
        return false;
      } else {
        handleError(error, "NPI 数据清零");
        return false;
      }
    }
  };

  /**
   * 通用数据清零
   * @param cpu CPU 编号
   * @param type 清零类型
   */
  const clearData = async (cpu: number = 1, type: NpiClearType) => {
    const isUserData = type === NpiClearType.USER_DATA;
    const dataTypeName = isUserData ? "用户数据" : "NPI 数据";

    try {
      // 显示确认对话框
      await ElMessageBox.confirm(
        `确定要执行${dataTypeName}清零操作吗？此操作不可撤销。`, 
        "确认清零", 
        {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        }
      );

      // 执行清零操作
      const result = await npiStore.clearData(cpu, type);

      // 根据响应结果提示用户
      if (result.status === "Success") {
        ElMessage.success(`${dataTypeName}清零成功`);
        return true;
      } else {
        ElMessage.error(`${dataTypeName}清零失败`);
        return false;
      }
    } catch (error: any) {
      if (error === "cancel") {
        ElMessage.info(`已取消${dataTypeName}清零操作`);
        return false;
      } else {
        handleError(error, `${dataTypeName}清零`);
        return false;
      }
    }
  };

  /**
   * 重置用户数据清零结果
   */
  const resetUserClearResult = () => {
    npiStore.resetUserClearResult();
  };

  /**
   * 重置 NPI 数据清零结果
   */
  const resetNpiClearResult = () => {
    npiStore.resetNpiClearResult();
  };

  /**
   * 重置所有清零结果
   */
  const resetAllClearResults = () => {
    npiStore.resetAllClearResults();
  };

  /**
   * 获取用户数据清零结果状态
   */
  const getUserClearStatus = () => {
    const result = lastUserClearResult.value;
    return result ? result.status === "Success" : null;
  };

  /**
   * 获取 NPI 数据清零结果状态
   */
  const getNpiClearStatus = () => {
    const result = lastNpiClearResult.value;
    return result ? result.status === "Success" : null;
  };

  return {
    // 状态
    isClearing,
    isClearingUser,
    isClearingNpi,
    lastUserClearResult,
    lastNpiClearResult,
    
    // 方法
    clearUserData,
    clearNpiData,
    clearData,
    resetUserClearResult,
    resetNpiClearResult,
    resetAllClearResults,
    getUserClearStatus,
    getNpiClearStatus,
  };
}
